/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cas.struts.action;

import com.cas.dbaccess.CASDBAccessfactory;
import com.cas.hibernate.entity.Country;
import com.cas.hibernate.entity.Doctor;
import com.cas.hibernate.entity.Patient;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts.util.LabelValueBean;

/**
 *
 * @author suraj
 */
public class EntityAction extends org.apache.struts.action.Action {

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String entityType = request.getParameter("entityType");
        String entityAction = request.getParameter("entityAction");
        String entityId = request.getParameter("entityId");
        String forward = "error";

        DynaValidatorActionForm formD = (DynaValidatorActionForm) form;
        formD.set("entityAction", entityAction);

        System.out.println("Entity Action Details : " + entityAction + " " + entityType + " " + entityId);

        if ("doctor".equals(entityType)) {
            if ("view".equals(entityAction)) {
                forward = viewDoctor(mapping, form, request, response, entityId);
            } else if ("edit".equals(entityAction)) {
                forward = editDoctor(mapping, form, request, response, entityId);
            } else if ("delete".equals(entityAction)) {
                forward = deleteDoctor(entityId);
            } else if ("add".equals(entityAction)) {
                formD.getMap().clear();
                formD.set("sex", "1");
                formD.set("entityAction", "add");
                forward = "viewDoctor";
            }
        } else if ("patient".equals(entityType)) {
            if ("view".equals(entityAction)) {
                forward = viewPatient(mapping, form, request, response, entityId);
            } else if ("edit".equals(entityAction)) {
                forward = editPatient(mapping, form, request, response, entityId);
            } else if ("delete".equals(entityAction)) {
                forward = deletePatient(entityId);
            } else if ("add".equals(entityAction)) {
                formD.getMap().clear();
                formD.set("sex", "1");
                formD.set("entityAction", "add");
                ArrayList<Country> countries = CASDBAccessfactory.getCASDbAccess().getAllCountries();
                List<LabelValueBean> countryList = new ArrayList<LabelValueBean>();

                for (Country c : countries) {
                    countryList.add(new LabelValueBean(c.getNationality(), c.getCode()));
                }
                formD.set("countryList", countryList);
                forward = "viewPatient";
            }
        }

        return mapping.findForward(forward);
    }

    private String deleteDoctor(String doctorId) {
        CASDBAccessfactory.getCASDbAccess().deleteDoctor(Long.parseLong(doctorId));
        return "viewDoctors";
    }

    private void prePopulateDoctorForm(ActionForm form, String entityId, String viewMode) {
        Doctor d = CASDBAccessfactory.getCASDbAccess().getDoctor(entityId);
        if (d != null) {
            DynaValidatorActionForm formDoctor = (DynaValidatorActionForm) form;
            formDoctor.set("id", Long.toString(d.getId()));
            formDoctor.set("firstName", d.getFirstName());
            formDoctor.set("secondName", d.getSecondName());
            formDoctor.set("familyName", d.getFamilyName());
            formDoctor.set("sex", Integer.toString(d.getSex()));
            formDoctor.set("speciality", d.getSpeciality());
            //formDoctor.set("viewMode", viewMode);
        }
    }

    private String viewDoctor(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response, String entityID) {
        prePopulateDoctorForm(form, entityID, "true");
        return "viewDoctor";
    }

    private String editDoctor(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response, String entityID) {
        prePopulateDoctorForm(form, entityID, "false");
        return "editDoctor";
    }

    private String deletePatient(String patientId) {
        CASDBAccessfactory.getCASDbAccess().deleteDoctor(Long.parseLong(patientId));
        return "viewPatients";
    }

    private void prePopulatePatientForm(ActionForm form, String entityId, String viewMode) {
        Patient p = CASDBAccessfactory.getCASDbAccess().getPatient(entityId);
        if (p != null) {
            DynaValidatorActionForm formPatient = (DynaValidatorActionForm) form;
            formPatient.set("id", Long.toString(p.getId()));
            formPatient.set("registrationNumber", p.getRegistrationNumber());
            formPatient.set("firstName", p.getFirstName());
            formPatient.set("secondName", p.getSecondName());
            formPatient.set("familyName", p.getFamilyName());

            ArrayList<Country> countries = CASDBAccessfactory.getCASDbAccess().getAllCountries();

            List<LabelValueBean> countryList = new ArrayList<LabelValueBean>();

            for (Country c : countries) {
                countryList.add(new LabelValueBean(c.getNationality(), c.getCode()));
            }

            if (p.getDateOfBirth() != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                formPatient.set("dateOfBirth", sdf.format(p.getDateOfBirth()));
            } else {
                formPatient.set("dateOfBirth", "");
            }

            if (p.getOmanIdCardNumber() != null) {
                formPatient.set("omanIdCardNumber", p.getOmanIdCardNumber().toString());
            } else {
                formPatient.set("omanIdCardNumber", "");
            }

            formPatient.set("nationality", p.getNationality());
            formPatient.set("countryList", countryList);
            formPatient.set("sex", Integer.toString(p.getSex()));
            //formDoctor.set("viewMode", viewMode);
        }
    }

    private String viewPatient(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response, String entityID) {
        prePopulatePatientForm(form, entityID, "true");
        return "viewPatient";
    }

    private String editPatient(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response, String entityID) {
        prePopulatePatientForm(form, entityID, "false");
        return "editPatient";
    }
}
